package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.chrisbanes.photoview.PhotoView;
import com.kongzue.dialog.v3.CustomDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.Config;
import com.yuanli.cosmeticmirror.utils.BitmapUtils;
import com.yuanli.cosmeticmirror.utils.FileUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;
import comenjoy.com.imageloadlibrary.GlideUtil;
import gdut.bsx.share2.FileUtil;
import gdut.bsx.share2.Share2;
import gdut.bsx.share2.ShareContentType;

/**
 * 照片详情界面
 */
public class PhotoInfoActivity extends BaseActivity {
    @BindView(R.id.photo_info_back)
    ImageView photoInfoBack;
    @BindView(R.id.photo_info_img)
    PhotoView photoInfoImg;
    @BindView(R.id.photo_info_delete)
    ImageView photoInfoDelete;
    @BindView(R.id.photo_info_save)
    TextView photoInfoSave;
    @BindView(R.id.photo_info_share)
    ImageView photoInfoShare;
    private String imgUrl;
    private String bitmap;
    private long mLastClickTime = 0;
    public final long TIME_INTERVAL = 1000L;
    private Uri fileUri;

    @Override
    protected int getContentView() {
        return R.layout.activity_photo_info;
    }

    @Override
    protected void init(Bundle bundle) {

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onBackPressed() {
        if (!TextUtils.isEmpty(bitmap)) {
            FileUtils.deleteFile(bitmap);
        }
        super.onBackPressed();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        bitmap = intent.getStringExtra("saveBitmap");
        if (!TextUtils.isEmpty(bitmap)) {
            photoInfoSave.setVisibility(View.VISIBLE);
//            photoInfoShare.setVisibility(View.GONE);
            GlideUtil.getInstance().loadImage(PhotoInfoActivity.this, bitmap, photoInfoImg);
        }
//        else {
//            photoInfoSave.setVisibility(View.GONE);
//            photoInfoShare.setVisibility(View.VISIBLE);
//        }
        imgUrl = intent.getStringExtra("imgUrl");
        if (!TextUtils.isEmpty(imgUrl)) {
            photoInfoShare.setVisibility(View.VISIBLE);
            fileUri = FileUtil.getFileUri(this, ShareContentType.IMAGE, new File(imgUrl));

            GlideUtil.getInstance().loadImage(PhotoInfoActivity.this, imgUrl, photoInfoImg);
        }
    }

    @Override
    protected void destroy() {

    }


    @OnClick({R.id.photo_info_back, R.id.photo_info_delete, R.id.photo_info_save, R.id.photo_info_share})
    public void onViewClicked(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastClickTime < TIME_INTERVAL) {
            // do something
            return;
        }
        mLastClickTime = nowTime;
        switch (view.getId()) {
            case R.id.photo_info_back:
                if (!TextUtils.isEmpty(bitmap)) {
                    FileUtils.deleteFile(bitmap);
                }
                finish();
                break;
            case R.id.photo_info_delete:
                getCustomDialog();
                break;
            case R.id.photo_info_save:
                finish();
                break;
            case R.id.photo_info_share:
                if (fileUri == null) {
                    showToast("图片错误，不能分享");
                    return;
                }
                new Share2.Builder(this)
                        // 指定分享的文件类型
                        .setContentType(ShareContentType.IMAGE)
                        // 设置要分享的文件 Uri
                        .setShareFileUri(fileUri)
                        // 设置分享选择器的标题
                        .setTitle("化妆镜")
                        .build()
                        // 发起分享
                        .shareBySystem();
                break;
            default:
                break;
        }
    }


    /**
     * 照片详情  删除
     *
     * @return
     */
    private CustomDialog getCustomDialog() {
        return CustomDialog.show(PhotoInfoActivity.this, R.layout.dialog_delete_favorite, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.favorite_enter);
            TextView btncancel = v.findViewById(R.id.favorite_cancel);
            btnOk.setOnClickListener(v1 -> {
                if (!TextUtils.isEmpty(bitmap)) {
                    boolean b = FileUtils.deleteFile(bitmap);
                    showToast(b ? "删除成功" : "删除失败");
                    finish();
                    return;
                }
                boolean b = FileUtils.deleteFile(imgUrl);
                showToast(b ? "删除成功" : "删除失败");
                dialog.doDismiss();
                Intent intent = new Intent();
                setResult(RESULT_OK, intent.putExtra("delete", imgUrl));
                finish();
            });
            btncancel.setOnClickListener(v1 -> dialog.doDismiss());
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }
}
